import Taro from "@tarojs/taro";

/**
 * 分享配置接口
 */
export interface ShareConfig {
  title: string;
  path: string;
  imageUrl?: string;
}

/**
 * 朋友圈分享配置接口
 */
export interface TimelineShareConfig {
  title: string;
  query?: string;
  imageUrl?: string;
}

/**
 * 分享工具类
 */
export class ShareUtils {
  // 默认分享图片
  static readonly DEFAULT_SHARE_IMAGE = "/assets/imgs/logo.jpeg";
  
  // 默认分享标题
  static readonly DEFAULT_TITLE = "共运大道 - 优质生活服务平台";

  /**
   * 获取当前页面信息
   */
  static getCurrentPageInfo() {
    const pages = Taro.getCurrentPages();
    const currentPage = pages[pages.length - 1];
    return {
      route: currentPage?.route || '',
      options: currentPage?.options || {}
    };
  }

  /**
   * 生成分享到好友的配置
   */
  static generateShareAppMessage(customConfig?: Partial<ShareConfig>): ShareConfig {
    const { route, options } = this.getCurrentPageInfo();
    
    let shareConfig: ShareConfig = {
      title: this.DEFAULT_TITLE,
      path: '/pages/home/index',
      imageUrl: this.DEFAULT_SHARE_IMAGE
    };

    // 根据不同页面自定义分享内容
    switch (route) {
      case 'pages/home/index':
        shareConfig = {
          title: '共运大道 - 发现优质生活服务',
          path: '/pages/home/index',
          imageUrl: this.DEFAULT_SHARE_IMAGE
        };
        break;
        
      case 'pages/cate/index':
        shareConfig = {
          title: '共运大道 - 精选分类服务',
          path: '/pages/cate/index',
          imageUrl: this.DEFAULT_SHARE_IMAGE
        };
        break;
        
      case 'pages/shop/index':
        shareConfig = {
          title: '共运大道 - 我的购物车',
          path: '/pages/home/index', // 分享购物车页面跳转到首页
          imageUrl: this.DEFAULT_SHARE_IMAGE
        };
        break;
        
      case 'pages/my/index':
        shareConfig = {
          title: '共运大道 - 加入我们一起享受优质服务',
          path: '/pages/home/index',
          imageUrl: this.DEFAULT_SHARE_IMAGE
        };
        break;
        
      default:
        // 商品详情页
        if (route.includes('pages-detail/detail/index')) {
          shareConfig = {
            title: '共运大道 - 精选优质商品',
            path: `/pages-detail/detail/index?id=${options.id || ''}`,
            imageUrl: this.DEFAULT_SHARE_IMAGE
          };
        }
        // 分类列表页
        else if (route.includes('pages-detail/cate/list-view')) {
          shareConfig = {
            title: '共运大道 - 分类商品列表',
            path: `/pages-detail/cate/list-view?id=${options.id || ''}`,
            imageUrl: this.DEFAULT_SHARE_IMAGE
          };
        }
        break;
    }

    // 添加推荐码（如果存在）
    const params = Taro.getCurrentInstance().router?.params;
    if (params?.code) {
      const separator = shareConfig.path.includes('?') ? '&' : '?';
      shareConfig.path += `${separator}code=${params.code}`;
    }

    // 合并自定义配置
    return { ...shareConfig, ...customConfig };
  }

  /**
   * 生成分享到朋友圈的配置
   */
  static generateShareTimeline(customConfig?: Partial<TimelineShareConfig>): TimelineShareConfig {
    const { route, options } = this.getCurrentPageInfo();
    
    let shareConfig: TimelineShareConfig = {
      title: '共运大道 - 优质生活服务平台，发现更多精彩！',
      query: '',
      imageUrl: this.DEFAULT_SHARE_IMAGE
    };

    // 根据不同页面自定义朋友圈分享内容
    switch (route) {
      case 'pages/home/index':
        shareConfig.title = '共运大道 - 发现优质生活服务，让生活更美好！';
        break;
        
      case 'pages/cate/index':
        shareConfig.title = '共运大道 - 精选分类服务，总有一款适合你！';
        break;
        
      default:
        if (route.includes('pages-detail/detail/index')) {
          shareConfig.title = '共运大道 - 发现这个超棒的商品，快来看看吧！';
          shareConfig.query = `id=${options.id || ''}`;
        }
        else if (route.includes('pages-detail/cate/list-view')) {
          shareConfig.title = '共运大道 - 精选分类商品，不容错过！';
          shareConfig.query = `id=${options.id || ''}`;
        }
        break;
    }

    // 添加推荐码（如果存在）
    const params = Taro.getCurrentInstance().router?.params;
    if (params?.code) {
      const separator = shareConfig.query ? '&' : '';
      shareConfig.query += `${separator}code=${params.code}`;
    }

    // 合并自定义配置
    return { ...shareConfig, ...customConfig };
  }

  /**
   * 手动触发分享
   */
  static showShareMenu() {
    Taro.showShareMenu({
      withShareTicket: true,
      showShareItems: ['wechatFriends', 'wechatMoment']
    });
  }

  /**
   * 隐藏分享菜单
   */
  static hideShareMenu() {
    Taro.hideShareMenu();
  }
}

/**
 * 页面分享Mixin - 可以在Vue组件中使用
 */
export const useShare = (customConfig?: {
  shareAppMessage?: Partial<ShareConfig>;
  shareTimeline?: Partial<TimelineShareConfig>;
}) => {
  const onShareAppMessage = () => {
    return ShareUtils.generateShareAppMessage(customConfig?.shareAppMessage);
  };

  const onShareTimeline = () => {
    return ShareUtils.generateShareTimeline(customConfig?.shareTimeline);
  };

  return {
    onShareAppMessage,
    onShareTimeline
  };
}; 